package com.ds.iot.comet;

import com.ds.context.JDSCometContext;
import com.ds.esb.util.EsbFactory;
import com.ds.jds.core.esb.EsbUtil;
import com.ds.server.eumus.ConfigCode;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

public class CometActionContextImpl implements JDSCometContext {

	private HttpServletRequest request;

	private HttpServletResponse response;
	
	private String SESSIONKEY ="sessionId";

	public Map sessionMap = new HashMap();

	public Map contextMap = new HashMap();

	
	public CometActionContextImpl(HttpServletRequest request,
			HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	public Object Par(String expression) {
		Object obj = null;
		try {
			obj = Par(expression, null);
		} catch (Exception e) {

		}
		return obj;
	}

	public Map getContext() {
		if (request!=null){
			contextMap.putAll(request.getParameterMap());
		}
		
		return contextMap;
	}

	@Override
	public Map getPagectx() {
		return null;
	}

	@Override
	public void setPagectx(Map<String, Object> map) {

	}

	@Override
	public Object getParams(String param) {
		String value = (String) request.getAttribute(param);

		if (value == null) {
			value = request.getParameter(param);
		}

		if (value == null) {
			if (request.getSession() != null
					&& request.getSession().getAttribute(param) != null) {
				value = request.getSession().getAttribute(param).toString();
			}
		}

		if (value == null) {
			Object obj = getContext().get(param);
			if (obj != null) {
				if (obj.getClass().isArray()) {
					Object[] objs = (Object[]) getContext().get(param);
					if (objs.length > 0) {
						value = objs[0].toString();
					}

				} else {
					value = obj.toString();
				}

			}

		}

		return value;
	}

	@Override
	public String getIpAddr() {
		String ip = request.getHeader("X-Forwarded-For");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	@Override
	public Map getSession() {
		HttpSession session = request.getSession(true);
		if (session!=null){
			sessionMap = (Map) session.getAttribute("sessionMap");
			if (sessionMap == null) {
				sessionMap = new HashMap();
				session.putValue("sessionMap", sessionMap);
			}
		}
		

		return sessionMap;
	}

	@Override
	public String getSessionId() {
		String sessionid = (String) this.getParams(JSESSIONID);
		
		//兼容旧版协议
		if (sessionid==null){
			sessionid=(String) this.getParams(SESSIONKEY);
		}
		
		
		//长连接需要根据SESSIONID 认证 不能直接创建
//		if (sessionid == null || sessionid.equals("")) {
//			sessionid = UUID.randomUUID().toString().toUpperCase();
//			request.getSession().setAttribute(JSESSIONID, sessionid);
//		}
		return sessionid;
	}

	@Override
	public <T>T Par(String expression, Class<T> clazz) {
		return Par(expression, clazz, null);
	}

	@Override
	public <T> T Par(Class<T> clazz) {
		return EsbUtil.parExpression(clazz);
	}

	@Override
	public <T>T Par(String expression, Class<T> clazz, Object source) {
		T o = 	EsbFactory.par(expression, contextMap,null,clazz);
		return o;
	}

	@Override
	public Object getHttpRequest() {
		return request;
	}

	

	public String getSystemCode() {
		String syscode = (String) this.getSession().get(SYSCODE);
		if (syscode == null) {
			syscode = (String) this.getParams(SYSCODE);
			if (syscode == null) {
			}

			this.getSession().put(SYSCODE, syscode);
		}

		return syscode;
	}

	@Override
	public ConfigCode getConfigCode() {
		return null;
	}

	@Override
	public void remove() {

	}

	public Object getHttpResponse() {
		return response;
	}

	public String getHost() {
		
	
		return request.getHeader("host");
	}

}
